/**
 * Created by PengWei on 2018/7/17.
 */
namespace mengdao {
    /**
     *  扩展安装
     *  执行传入的类（或类定义）的实例的extend方法
     *  每个类型的扩展安装确保只有一次
     */
    export class ExtensionInstaller {

        private _classes: Map<any, any> = new Map<any, any>();
        private _context: Context;

        public constructor(context: Context) {
            this._context = context;
        }

        public install(extension: any): void {
            if (typeof extension === "function" && extension.prototype.extend !== undefined) {
                if (!this._classes.has(extension)) {
                    this.install(new extension());//ask 此处的递归不是必须的，建议把下面的内容拆出，以提高性能与内存
                }
            } else {
                let extensionClass: any = <any>extension.constructor;
                if (!this._classes.has(extensionClass)) {
                    this._classes.set(extensionClass, true);
                    extension.extend(this._context);
                }
            }
        }

        public destroy(): void {
            this._classes.clear();
        }
    }
}